package com.apes.hr.salary.rewardPunish.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.EsbRouter;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.repository.EmployeeRepository;
import com.apes.hr.salary.rewardPunish.model.PunishAppeal;
import com.apes.hr.salary.rewardPunish.repository.PunishAppealRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.rbac.model.Post;
import com.apes.scm.rbac.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 功能：处惩申诉服务
 *
 * @author LinWangZong
 * @create 2018-12-03 16:35
 */
@Service("punishAppealService")
public class PunishAppealService extends DomainService {

    @Autowired
    PunishAppealRepository punishAppealRepository;
    @Autowired
    PostRepository postRepository;
    @Autowired
    EmployeeRepository employeeRepository;

    /**
     * 功能：保存处惩申诉
     * @param request
     * @return
     */
    public PunishAppeal save(SimpleRequest request) {
        PunishAppeal punishAppeal = request.getO(PunishAppeal.class);

        punishAppeal.setState("create");
        return punishAppealRepository.saveAndFlush(punishAppeal);
    }


    /**
     * 功能：保存处惩申诉(员工)
     * @param request
     * @return
     */
    public PunishAppeal create(SimpleRequest request) {
        PunishAppeal punishAppeal = request.getO(PunishAppeal.class);
        Employee employee = employeeRepository.findByParty(((PartyRole)request.getPerson()).getParty());
        if (employee==null) throw new RuntimeException("当前登录员没有对应的人员档案!");
        punishAppeal.setEmployee(employee);
        punishAppeal.setState("create");
        return punishAppealRepository.saveAndFlush(punishAppeal);
    }

    /**
     * 功能：检查数据
     * @param request
     */
    public PunishAppeal checkData(SimpleRequest request){
        String id = request.get("id");
        int version = request.get("version");

        PunishAppeal oldBill = punishAppealRepository.findOne(id);
        if (oldBill == null) throw new RuntimeException("奖惩记录：" + request.get("id") + " 在系统中不存在。");
        if (oldBill.getVersion() != version ) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return oldBill;
    }

    /**
     * 功能：修改数据
     * @param request
     * @return
     */
    public PunishAppeal update(SimpleRequest request){
        PunishAppeal oldBill = checkData(request);

        return save(request);
    }

    /**
     * 功能：删除处惩申诉
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        PunishAppeal oldBill = checkData(request);
        if (oldBill.getApproveDate() != null) throw new RuntimeException("申诉：" + request.get("id") + " 在系统中已审批。");

        oldBill.setState("delete");
        punishAppealRepository.saveAndFlush(oldBill);
        return "删除成功！";
    }

    /**
     * 功能：取消处惩申诉
     * @param request
     * @return
     */
    public PunishAppeal cancel(SimpleRequest request) {
        PunishAppeal oldBill = checkData(request);
        if (oldBill.getCancelDate() != null) throw new RuntimeException("申诉：" + request.get("id") + " 在系统中已取消！");

        oldBill.setCancelDate(new Date());
        oldBill.setCancelUid(this.getPerson(request));

        return punishAppealRepository.saveAndFlush(oldBill);
    }

    /**
     * 功能：审核处惩申诉
     *
     * @param request
     * @return
     */
    public PunishAppeal approve(SimpleRequest request) {
        PunishAppeal oldBill = checkData(request);
        if (oldBill.getApproveDate() != null) throw new RuntimeException("申诉：" + request.get("id") + " 在系统中已审核。");

        oldBill.setState("approve");
        oldBill.setApproveDate(new Date());
        oldBill.setApproveUid(this.getPerson(request));
        return punishAppealRepository.save(oldBill);
    }
    /**
     * 功能：驳回处惩申诉
     *
     * @param request
     * @return
     */
    public PunishAppeal reject(SimpleRequest request) {
        PunishAppeal oldBill = checkData(request);
        oldBill.setState("reject");
        oldBill.setCancelDate(new Date());
        oldBill.setCancelUid(this.getPerson(request));
        return punishAppealRepository.save(oldBill);
    }
    public PunishAppeal findOne(SimpleRequest request) throws Exception {
        String id = request.get("id");
        return punishAppealRepository.findOne(id);
    }

    public Page<PunishAppeal> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return punishAppealRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    /**
     * 查询自己的申述记录
     * @param request
     * @return
     */
    public Page<PunishAppeal> findByEmployee(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Employee employee = employeeRepository.findByParty(((PartyRole)request.getPerson()).getParty());
        JSONObject djzt = new JSONObject();
        djzt.put("field","employee");
        djzt.put("operator","EQ");
        djzt.put("value",employee);
        filters.add(djzt);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return punishAppealRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size"), sort));
    }

    /***
     * 功能 转OA start 节点
     * @param request
     * @return
     * @throws RuntimeException
     */
    public JSONObject transOA(SimpleRequest request) throws RuntimeException{
        //设置单据状态为处理中
        PunishAppeal punishAppeal = request.getO(PunishAppeal.class);
        Employee employee =punishAppeal.getEmployee();
        Post post = postRepository.findOne(request.getPostId());
        JSONObject tempJson = request.getJO();
        punishAppeal.setState("disposing");  //设置当前的状态为处理中
        String companyId;
        if (employee.getCompany()!=null){
            companyId = employee.getCompany().getId();
        }else {
            companyId = post.getCompany().getId();//转OA人员角色的所属公司
        }
        tempJson.put("company",companyId);
        punishAppealRepository.saveAndFlush(punishAppeal);
        return  tempJson;
    }



}
